<template>
  <div class="login">
    <div class="login-box">
      <div class="sys-title">EHR SYSTEM</div>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="60px"
        class="demo-ruleForm"
        :size="formSize"
        status-icon
      >
        <el-form-item label="账 号" prop="email">
          <el-input :prefix-icon="User" v-model="ruleForm.email" />
        </el-form-item>
        <el-form-item label="密 码" prop="passWord">
          <el-input
            type="password"
            show-password
            :prefix-icon="Lock"
            v-model="ruleForm.passWord"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import type { FormInstance, FormRules } from "element-plus";

import { signIn } from "@/api/login";

import { useRouter } from "vue-router";
const router = useRouter();

const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive({
  email: "",
  passWord: "",
});

const rules = reactive<FormRules>({
  email: [{ required: true, message: "请输入账号", trigger: "blur" }],
  passWord: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const res = await signIn(ruleForm);
      router.push({
        path: "/home",
      });
      sessionStorage.setItem("token", res.token);
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>
<style lang="stylus" scoped>
.login
    height 100%
    width 100%
    background-image url('~@/assets/bg.jpg')
    background-size 100% 100%
    .login-box
        width 500px
        height: 300px
        position absolute
        top 50%
        left: 50%
        margin-left: -250px
        margin-top: -150px
        border: 1px solid #efefef
        border-radius: 5px
        background-color: rgba(0,0,0,0.3)
        padding: 50px
        // padding-left: 0
        // padding-top: 100px
        box-sizing: border-box
        .sys-title
            font-size 30px
            color #fff
            text-align: center
            margin-bottom: 30px
        .el-form
            :deep(.el-form-item__label)
                color #fff
        .el-button
            width 100%
</style>
